package persistence.impl;

import domain.CartItem;
import domain.Item;
import persistence.CartDao;
import persistence.DBUtil;
import persistence.ItemDao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    private static final String INSERT_LINEITEM_IN_CART=
            "INSERT INTO CART(USERID,ITEMID,QUANTITY,LISTPRICE,TOTALPRICE,ISEXIST)" +
                    "VALUES(?,?,?,?,?,?)";
    private static final String REMOVE_LINEITEM_FROM_CART=
            "UPDATE CART SET ISEXIST = 0,QUANTITY=0,TOTALPRICE=0 WHERE ITEMID=? AND USERID=?";
    private static final String GET_CART_LINEITEM_FROM_USERID=
            "SELECT USERID,ITEMID,QUANTITY,LISTPRICE,TOTALPRICE,ISEXIST FROM CART WHERE USERID =?";
    private static final String UPDATE_CART=
            " UPDATE CART SET QUANTITY =?,ISEXIST=?,TOTALPRICE=LISTPRICE*? WHERE ITEMID =? AND USERID=?";

    private static final String INCREASE_CARTITEM_QUANTITY=
            "UPDATE CART SET QUANTITY=QUANTITY+1,TOTALPRICE=TOTALPRICE+LISTPRICE WHERE ITEMID=?AND USERID=?";
    private static final String SELECT_ITEM_IN_CART=
            "SELECT USERID,ITEMID,ISEXIST FROM CART WHERE ITEMID=? AND USERID=?";
    private static final String INSERT_EXIST_ITEM_IN_CART=
            "UPDATE CART SET QUANTITY=QUANTITY+?,ISEXIST=?,TOTALPRICE=TOTALPRICE+? WHERE ITEMID=? AND USERID=?";


    @Override
    public void insertCartItemInCart(CartItem cartItem, String userid) {
        try{

            Connection connection= DBUtil.getConnection();
            PreparedStatement preparedStatement= connection.prepareStatement(INSERT_LINEITEM_IN_CART);
            preparedStatement.setString(1,userid);
            preparedStatement.setString(2,cartItem.getItem().getItemId());
            preparedStatement.setInt(3,cartItem.getQuantity());
            preparedStatement.setBigDecimal(4,cartItem.getItem().getListPrice());
            preparedStatement.setBigDecimal(5,cartItem.getTotal());
            preparedStatement.setBoolean(6,true);
            preparedStatement.executeUpdate();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch(Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public void removeCartItemFromCart(String itemid, String userid) {
        try{
            Connection connection=DBUtil.getConnection();
            PreparedStatement preparedStatement= connection.prepareStatement(REMOVE_LINEITEM_FROM_CART);
            preparedStatement.setString(1,itemid);
            preparedStatement.setString(2,userid);
            preparedStatement.executeUpdate();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);

        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public List<CartItem> getCartItemFromUserId(String userid) {
        List<CartItem> list=new ArrayList<>();
        try{
            Connection connection=DBUtil.getConnection();
            PreparedStatement preparedStatement= connection.prepareStatement(GET_CART_LINEITEM_FROM_USERID);
            preparedStatement.setString(1,userid);
            ResultSet resultSet= preparedStatement.executeQuery();
            while(resultSet.next()){
                boolean isexist=resultSet.getBoolean("isexist");
                if(!isexist){
                    continue;
                }
                CartItem cartItem=new CartItem();
                ItemDao itemDao=new ItemDaoImpl();

                Item item=itemDao.getItem(resultSet.getString("itemid"));

                cartItem.setItem(item);
                cartItem.setQuantity(resultSet.getInt("quantity"));
                list.add(cartItem);

            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch(Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public void updateCart(String userid, String itemid, int quantity) {
        try{
            Connection connection=DBUtil.getConnection();
            PreparedStatement preparedStatement= connection.prepareStatement(UPDATE_CART);
            if(quantity<1){
                preparedStatement.setInt(1,0);
                preparedStatement.setBoolean(2,false);
                preparedStatement.setInt(3,0);
            }else{
                preparedStatement.setInt(1,quantity);
                preparedStatement.setBoolean(2,true);
                preparedStatement.setInt(3,quantity);
            }
            preparedStatement.setString(4,itemid);
            preparedStatement.setString(5,userid);
            preparedStatement.executeUpdate();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void insertListInCart(List<CartItem> list, String userid) {
        Boolean b=true;
        for(int i=0;i<list.size();i++){
            b=isItemInCart(list.get(i).getItem().getItemId(),userid);
            if(b){
                insertExistItemInCaart(list.get(i),userid);
            }else{
                insertCartItemInCart(list.get(i),userid);
            }
        }
    }

    @Override
    public void increaseQuantityByItemId(String itemid,String userid) {
        try{
            Connection connection=DBUtil.getConnection();
            PreparedStatement preparedStatement= connection.prepareStatement(INCREASE_CARTITEM_QUANTITY);
            preparedStatement.setString(1,itemid);
            preparedStatement.setString(2,userid);
            preparedStatement.executeUpdate();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public boolean isItemInCart(String itemid, String userid) {
        boolean b=false;
        try{
            Connection connection=DBUtil.getConnection();
            PreparedStatement preparedStatement=connection.prepareStatement(SELECT_ITEM_IN_CART);
            preparedStatement.setString(1,itemid);
            preparedStatement.setString(2,userid);
            ResultSet resultSet=preparedStatement.executeQuery();
            if(resultSet.next()){
                System.out.println("true");
                b=true;
            }else{
                System.out.println("false");
                b=false;
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);

        }catch (Exception e){
            e.printStackTrace();
        }

        return b;
    }

    @Override
    public void insertExistItemInCaart(CartItem cartItem, String userid) {
        try{
            Connection connection=DBUtil.getConnection();
            PreparedStatement preparedStatement=connection.prepareStatement(INSERT_EXIST_ITEM_IN_CART);
            preparedStatement.setInt(1,cartItem.getQuantity());
            System.out.println("quantity="+cartItem.getQuantity());
            preparedStatement.setBoolean(2,true);
            preparedStatement.setBigDecimal(3,cartItem.getTotal());
            System.out.println("totalprice="+cartItem.getTotal());
            preparedStatement.setString(4,cartItem.getItem().getItemId());
            preparedStatement.setString(5,userid);

            preparedStatement.executeUpdate();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
